<?php

namespace app\common\model;

use think\Model;
use think\Session;
use think\Validate;

/**
 * Created by PhpStorm.
 * Author: Masky
 * Date: 2017/11/1
 * Time: 16:20
 */
class ViewBlockContent extends Model {
	
	const STATUS_CLOSE=0;
	const STATUS_OPEN=1;
	
	public static $status=[
		self::STATUS_CLOSE=>'关闭',
		self::STATUS_OPEN=>'开启'
	];
	
	public function addVBC($input){
		$validate=new Validate(['title'=>'require','vb_id'=>'require']);
		$result=FALSE;
		if($validate->check($input)){
			$input['created_at']=time();
			$result=$this->create($input);
			$result || $this->error="添加失败";
		}else{
			$this->error=$validate->getError();
		}
		return $result;
	}
	
	public function editVBC($input){
		$validate=new Validate(['title'=>'require','vb_id'=>'require']);
		$result=FALSE;
		if($validate->check($input)){
			$result=$this->save($input,['id'=>$input['id']]);
			$result || $this->error="编辑失败";
		}else{
			$this->error=$validate->getError();
		}
		return $result;
	}
	
	public function getInfo($condition=[],$field='*',$order='id desc'){
		
		$result=$this->where($condition)->field($field)->order($order)->find();
		$result && $this->formatSelect($result);
		return $result;
	}
	
	public function formatSelect(&$data){
		$data['org_url']=$data['url'];
		$data['url'] || $data['url']="/home/index/contentDetail/id/".$data['id'];
		$data['created_at_dis']=date("Y-m-d H:i:s",$data['created_at']);
		$data['status_dis']=self::$status[$data['status']];
		$data['org_content']=$data['content'];
		$data['content']=str_replace('&nbsp;','',strip_tags($data['content']));
	}
	public function getList($condition=[],$field='*',$order='sort desc,id desc',$size=15){
		
		$result=$this->where($condition)->field($field)->order($order)->paginate($size,false,['query' =>input()]);
		if($result){
			foreach ($result as &$item){
				$this->formatSelect($item);
			}
		}
		return $result;
	}
	
	public function deleteVBC($condition){
		$result=$this->where($condition)->select();
		if($result){
			foreach($result as $item){
				$this->where(['id'=>$item['id']])->delete();
			}
		}
		return TRUE;
	}
}